<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:mo="https://gitee.com/aun/tentime">
<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})">
    <style type="text/css">
        .layui-form-radio {margin: 0px 5px 0 0 !important;}
    </style>
</head>
<body>
<div class="layui-form tentime-compile">
    <form id="inputForm" th:object="${entity}" class="layui-form">
        <input id="id" type="hidden" name="id" th:if="${entity}" th:value="${entity?.id}"/>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">类型</label>
                <div class="layui-input-inline">
                    <select name="type" lay-verify="required" mo:dict="COUPON_TYPE" mo-selected="${entity?.type}" mo-empty=""></select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label required">名称</label>
                <div class="layui-input-inline">
                    <input name="name" class="layui-input" lay-verify="required" type="text" placeholder="请输入" th:value="${entity?.name}">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
	            <label class="layui-form-label required">优惠金额</label>
	            <div class="layui-input-inline">
	                <input class="layui-input" lay-verify="required" type="text" name="money" placeholder="请输入优惠金额/点值" th:value="${entity?.money}">
	            </div>
            </div>
            <div class="layui-inline">
	            <label class="layui-form-label required">优惠折扣</label>
	            <div class="layui-input-inline">
	                <input class="layui-input" lay-verify="required" type="text" name="dis" placeholder="请输入优惠折扣" th:value="${entity?.dis}">
	            </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
	            <label class="layui-form-label required">预发数量</label>
	            <div class="layui-input-inline">
	                <input class="layui-input" lay-verify="required|integer" type="text" name="cnt" placeholder="请输入发行数量" th:value="${entity?.cnt}">
	            </div>
            </div>
            <div class="layui-inline">
	            <label class="layui-form-label required">使用条件</label>
	            <div class="layui-input-inline">
	                <div style="float: left;padding: 5px 5px;">满</div>
                    <div style="float: left">
                        <input class="layui-input" lay-verify="required|number" type="text" name="cond" placeholder="请输入使用条件" th:value="${entity?.cond}" style="width: 160px;">
                    </div>
                    <div style="float: right;padding: 5px 5px;">元可用</div>
	            </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline" id="during_date">
                <label class="layui-form-label">生效日期</label>
                <div class="layui-input-inline" style="width: 110px;">
                    <input type="text" autocomplete="off" name="effDate" id="effDate" class="layui-input" placeholder="开始日期" th:value="${#dates.format(entity?.effDate,'yyyy-MM-dd')}" style="width: 105px;">
                </div>
                <div class="layui-form-mid" style="padding: 0px!important;">-</div>
                <div class="layui-input-inline" style="width: 110px;">
                    <input type="text" autocomplete="off" name="expDate" id="expDate" class="layui-input" placeholder="结束日期" th:value="${#dates.format(entity?.expDate,'yyyy-MM-dd')}" style="width: 105px;">
                </div>
            </div>
            <div class="layui-inline" style="width: 420px;">
                <label class="layui-form-label">领取规则</label>
                <div class="layui-input-inline" style="width: 300px;">
                    <input type="radio" name="gain" value="0" title="不限" th:checked="${entity?.gain} eq 0">
                    <input type="radio" name="gain" value="1" title="仅限一次" th:checked="${entity?.gain} eq 1">
                    <input type="radio" name="gain" value="2" title="每天一次" th:checked="${entity?.gain} eq 2">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">适用店铺</label>
                <div class="layui-input-inline">
                    <div id="userStore_xm" style="width: 600px"></div>
                    <input id="userStore" name="userStore" class="layui-input" type="text" th:value="${entity?.userStore}" style="display: none">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">适用会籍</label>
                <div class="layui-input-inline">
                    <div id="userMember_xm" style="width: 600px"></div>
                    <input id="userMember" name="userMember" class="layui-input" type="text" th:value="${entity?.userMember}" style="display: none">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remarks" style="max-width: 600px;width:600px;">[[${entity?.remarks}]]</textarea>
            </div>
        </div>
        <div class="layui-form-item tentime-finally">
            <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit lay-filter="submit_button" shiro:hasAnyPermissions="cars:coupon:add,cars:coupon:edit"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="closeWin();" type="button"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script th:replace="/common/template :: common"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['form','layer', 'laydate', 'xmSelect'], function () {
        window.form = layui.form;
        layer = layui.layer,laydate = layui.laydate, xmSelect = layui.xmSelect;
        //日期
        //laydate.render({elem: '#effDate'});
        //laydate.render({elem: '#expDate'});
        laydate.render({elem: '#during_date' ,range: ['#effDate', '#expDate']});

        var userStore_xm = xmSelect.render({
            el: '#userStore_xm',
            theme: {color: '#f37b1d'},
            filterable: true,
            toolbar: { show: true },
            autoRow: true,
            height: '500px',
            data: []
        });
        $.ajax({
            url: ctx + "/cars/store/listStore",
            type: "get",
            dataType:"json",   //返回格式为json
            // data: {data:data},
            success: function(data) {
                var res = data;
                userStore_xm.update({
                    data: res.data,
                    autoRow: true,
                });
                // 因为是异步，必须在初始化后赋值
                var userStore_v = $('#userStore').val();
                if (userStore_v != '') userStore_xm.setValue($.parseJSON(userStore_v));
            },
            error: function() {
                layer.msg('无法获取店铺信息，请联系管理员！');
            }
        });

        var userMember_xm = xmSelect.render({
            el: '#userMember_xm',
            theme: {color: '#f37b1d'},
            filterable: true,
            toolbar: { show: true },
            autoRow: true,
            height: '500px',
            data: []
        });

        $.ajax({
            url: ctx + "/cars/member/listMember",
            type: "get",
            dataType:"json",   //返回格式为json
            // data: {data:data},
            success: function(data) {
                var res = data;
                userMember_xm.update({
                    data: res.data,
                    autoRow: true,
                });
                // 因为是异步，必须在初始化后赋值
                var userMember_v = $('#userMember').val();
                if (userMember_v != '') userMember_xm.setValue($.parseJSON(userMember_v));
            },
            error: function() {
                layer.msg('无法获取会籍信息，请联系管理员！');
            }
        });

        form.on("submit(submit_button)", function (data) {
            var select_store = userStore_xm.getValue(), select_member = userMember_xm.getValue();
            $('#userStore').val((select_store == '') ? '' : JSON.stringify(select_store, null, 2));
            $('#userMember').val((select_member == '') ? '' : JSON.stringify(select_member, null, 2));
	        $("#inputForm").ajaxSubmit({
				type : "post"
	            ,url : ctx + '/cars/coupon/saveEntity'
	            ,success : function(data) {
	            	var code = data.code;
	            	if (code == 200) {
	            		parent.layer.msg('数据保存成功!', {icon: 1});
	            		closeWin();
	            		window.parent.reloadTable();
	            	} else {
	            		parent.layer.msg('数据验证失败!', {icon: 2});
	            	}
	            }
			});
       		return false;
       	});
    });

</script>
</body>
</html>